(EDITOR'S NOTE: this tutorial was created by UnConeD to help someone create a complicated raytraced object.  once you understand his raytracing article you can look at this to help understand multiple shapes and shapes more complicated than planes)

The goal here is to create a sphere with a tunnel in it. Or mathematically, a sphere with a cylinder subtracted from it. 

The equation for a sphere around the origin is: 

x^2 + y^2 + z^2 = R^2 

with R being the radius 

To raytrace, we replace each component x,y,z with the equation for the ray (k*dx+ox,k*dy+oy,k*dz+oz): 

(k*dx+ox)^2 + (k*dy+oy)^2 + (k*dz+oz)^2 = R^2 

Now you work out the brackets and separate everything by 'k': 

(dx^2 + dy^2 + dz^2)*k^2 + 2*(dx*ox+dy*oy+dz*oz)*k + (ox^2 + oy^2 + oz^2 - R^2) = 0 

This is a quadratic (second power) equation in 'k', which we can write shorthard as: 

ak^2 + bk + c = 0 

We know from math that the solution can be found from the discriminant: 
D = b^2 - 4*a*c 

so that: 

k = (-b - sqrt(D)) / (2*a) 
k = (-b + sqrt(D)) / (2*a) 

Because this is a quadratic equation, there are two solutions. 
This is logical, because when you draw a line through a sphere, it has to intersect in two points, or none (or two identical points when it's tangent). 
In the second case, 'd' will be negative, so its squareroot will not exist. This means that the ray does not intersect the sphere (this is only possible if we are outside the sphere). 
The best approach is to use sqrt(abs(d)) and set alpha to 0 when d < 0. This results in the least amount of ugly edges around the sphere. 

Once you've found 'k', you can find the intersection point from one of the k's: 

px = k*dx + ox; 
py = k*dy + oy; 
pz = k*dz + oz; 
(px,py,pz) 

Depending on which k you choose, you will have the point at the back or at the front of the sphere. 

To find the correct (x,y) pair to map onto the sphere, you can use: 


x = atan2(pz,px)/(2*pi); (pi is 3.1415...) 
y = asin(py/R)*2/pi; (R is the radius of the sphere) 


Things to note: 
- The sphere is always around the origin, it is only the camera/ray that moves 
- If the camera is inside the sphere, one of the k's will be negative (behind the camera) and should not be used 

------------ 

Now to make your tunnel, here's how I would do it... 

Because the sphere and cylinder (tunnel) have front and backsides, a simple k=min(k1,k2) won't do (where k1 and k2 are the k's found by raytracing the sphere and cylinder). 

So I suggest you raytrace the sphere first and calculate the px,pz coordinates of the intersection point (the cylinder lies on the y-axis). You then check sqrt(sqr(px)+sqr(pz)). If it it smaller than the radius of the cylinder, you re-raytrace this point, but with the cylinder equation. 
The idea is like taking an apple and removing the hard center (which you don't eat) without cutting it into pieces. 

Finally, you will need to make sure the tunnel is only as long as the sphere: 

	-------------------------------------------------------------------------------- 
	    .     . <- if the points on the sphere lie within this cylinder, retrace them
	    |-----|
	    .     .
	   _.     ._     ___________
	  / |     | \               |
	 /  |     |  \              |
	|   |     |   |             |
	|   |     |   |             |
	 \  |     |  /              |   if the resulting cylinder point is outside
	  \_|     |_/    ___________|   the sphere (check the y coordinate), then you set 	alpha to 0.
	    .     .
	    .     .
	    .     .
	    .     .
	--------------------------------------------------------------------------------


Attachment shows: 
1) the situation as it is (no combinations) 
2) min operation on k1,k2 
3) max operation on k1,k2 
-- 
4) raytracing a sphere 
5) retracing only the points inside the cylinder's volume (notice the rightmost red point has shifted into the shaft, now in the correct position)